package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type2_formula4_location_activity.radioButton
import kotlinx.android.synthetic.main.type2_formula4_location_activity.radioButton2
import kotlinx.android.synthetic.main.type2_formula5_location_activity.*
import kotlinx.android.synthetic.main.type2_formula6_location_activity.*
import java.lang.NumberFormatException

/**
 *  声学 - 干涉频率与距离差
 */
class Type2Formula6Activity : BaseActivity() {

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type2Formula6Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type2_formula6_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula2_6_s1_TEV.reset()
            formula2_6_s2_TEV.reset()
            formula2_6_v_TEV.reset()
            formula2_6_d_TEV.reset()
            formula2_6_t_TEV.reset()
            formula2_6_f1_TEV.reset()
            formula2_6_f2_TEV.reset()
            formula2_6_f3_TEV.reset()
            formula2_6_f4_TEV.reset()
            formula2_6_f_TEV.reset()
            formula2_6_d2_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                val resultD = FormulaUtil.formula2_6_1(
                    formula2_6_s1_TEV.getDouble(),
                    formula2_6_s2_TEV.getDouble()
                )
                val resultT = FormulaUtil.formula2_6_2(resultD, formula2_6_v_TEV.getDouble())
                val resultF1 = FormulaUtil.formula2_6_3(formula2_6_v_TEV.getDouble(), resultD, 0.5)
                val resultF2 = FormulaUtil.formula2_6_3(formula2_6_v_TEV.getDouble(), resultD, 1.5)
                val resultF3 = FormulaUtil.formula2_6_3(formula2_6_v_TEV.getDouble(), resultD, 2.5)
                val resultF4 = FormulaUtil.formula2_6_3(formula2_6_v_TEV.getDouble(), resultD, 3.5)
                val resultD2 = FormulaUtil.formula2_6_4(
                    formula2_6_v_TEV.getDouble(),
                    formula2_6_f_TEV.getDouble()
                )
                formula2_6_d_TEV.setText(resultD)
                formula2_6_t_TEV.setText(resultT)
                formula2_6_f1_TEV.setText(resultF1)
                formula2_6_f2_TEV.setText(resultF2)
                formula2_6_f3_TEV.setText(resultF3)
                formula2_6_f4_TEV.setText(resultF4)
                formula2_6_d2_TEV.setText(resultD2, 5)
            } catch (e: NumberFormatException) {
                formula2_6_d_TEV.setText("Infinity")
                formula2_6_t_TEV.setText("Infinity")
                formula2_6_f1_TEV.setText("Infinity")
                formula2_6_f2_TEV.setText("Infinity")
                formula2_6_f3_TEV.setText("Infinity")
                formula2_6_f4_TEV.setText("Infinity")
                formula2_6_d2_TEV.setText("Infinity")
            }
        }
    }
}